﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace DBProject
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    ///
    public partial class MainWindow : Window
    {
        private List<PersonDBEntry> _peopleList;

        public MainWindow()
        {
            InitializeComponent();
            Uri iconUri = new Uri("database-512.ico", UriKind.RelativeOrAbsolute);
            this.Icon = BitmapFrame.Create(iconUri);
            this._peopleList = new List<PersonDBEntry>();
        }

        public void AddEntry(string fN, string lN, string age, string eM, string pNo)
        {
            MessageBox.Show(string.Format("{0}, {1}, {2}, {3}, {4}", fN, lN, age, eM, pNo));
            PersonDBEntry addedPerson = new PersonDBEntry() { FirstName = fN, LastName = lN, Age = Convert.ToUInt16(age), Email = eM, PhoneNumber = Convert.ToUInt32(pNo) };
            this._peopleList.Add(addedPerson);
        }

        private void loadBtn_Click(object sender, RoutedEventArgs e)
        {
            string DBFile = "";
            // Create OpenFileDialog
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            // Set filter for file extension and default file extension
            dlg.DefaultExt = ".txt";
            dlg.Filter = "Text Files (*.txt)|*.txt|Comma-Separated Files (*.csv)|*.csv";

            // Display OpenFileDialog by calling ShowDialog method
            Nullable<bool> result = dlg.ShowDialog();

            // Get the selected file name and display in a TextBox
            if (result == true)
            {
                // Open document
                DBFile = dlg.FileName;
            }

            if (File.Exists(DBFile))
            {
                try
                {
                    this._peopleList = new List<PersonDBEntry>();

                    using (StreamReader sReader = new StreamReader(File.OpenRead(DBFile)))

                        while (!sReader.EndOfStream)
                        {
                            string line = sReader.ReadLine();
                            var values = line.Split(';');
                            AddEntry(values[0], values[1], values[2], values[3], values[4]);
                        }
                    peopleGrid.ItemsSource = _peopleList;
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.ToString());
                }
            }
            else
            {
                MessageBox.Show("File Not Specified");
            }
        }

        private void saveBtn_Click(object sender, RoutedEventArgs e)
        {
            string saveFile = "";

            // Create SaveFileDialog
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();

            // Set filter for file extension and default file extension
            dlg.DefaultExt = ".txt";
            dlg.Filter = "Text Files (*.txt)|*.txt|Comma-Separated Files (*.csv)|*.csv";

            // Display SaveFileDialog by calling ShowDialog method
            Nullable<bool> result = dlg.ShowDialog();

            // Get the selected file name
            if (result == true)
            {
                // Save document
                saveFile = dlg.FileName;
                using (StreamWriter sWriter = new StreamWriter(saveFile))
                    foreach (PersonDBEntry entry in this._peopleList)
                    {
                        sWriter.WriteLine(entry.WriteFileEntry);
                    }
            }
        }

        private void addPerson_Click(object sender, RoutedEventArgs e)
        {
            AddEntry addEntryWindow = new AddEntry();

            if (addEntryWindow.ShowDialog() != null && addEntryWindow.ShowDialog() != false)
            {
                this._peopleList.Add(addEntryWindow.addedPerson);
            }

            peopleGrid.Items.Refresh();
        }

        private void peopleGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }

        private void newButton_Click(object sender, RoutedEventArgs e)
        {
            this._peopleList = new List<PersonDBEntry>();
            peopleGrid.ItemsSource = _peopleList;
        }
    }
}